/*
 * @Desc:api请求封装
 * @author cheegon
 * @date 2022/7/25  10:54
**/
import request from './requestConfig.js'
import qs from 'qs'
// 预处理post
export const apiPost = (url, params) => {
  return new Promise((resolve, reject) => {
    request.post(url, params).then(res => {
      resolve(res.data)
    }).catch(res => {
      reject(res)
    })
  })
}

// post 的 query方式请求
export const apiQuery = (url, params) => {
  return new Promise((resolve, reject) => {
    request({
      url: url,
      method: 'post',
      params: params
    }).then(res => {
      resolve(res.data)
    }).catch(res => {
      reject(res)
    })
  })
}

// 预处理get
export const apiGet = (url, params) => {
  return new Promise((resolve, reject) => {
    request({
      url: url,
      method: 'get',
      params: params,
      paramsSerializer: {
        serialize:function(params) {
          return qs.stringify(params, {arrayFormat: 'repeat'})
        }
      }
    }).then(res => {
      resolve(res.data)
    }).catch(res => {
      reject(res)
    })
  })
}

// 上传图片的配置
export const apiUpload = (url, params) => {
  return new Promise((resolve, reject) => {
    request.get(url, params).then(res => {
      resolve(res.data)
    }).catch(res => {
      reject(res)
    })
  })
}
/**
 * post方法下载blob类型数据的文件
 * @param url 下载地址,不需要域名
 * @param params
 * @returns {Promise<unknown>}
 */
export const apiBlob = (url, params) => {
  return new Promise((resolve, reject) => {
    request({
      url: url,
      method: 'post',
      data: params,
      timeout: 30000,
      responseType: 'blob'
    }).then(res => {
      resolve(res)
    }).catch(res => {
      reject(res)
    })
  })
}
/**
 * get方法下载blob类型数据的文件
 * @param url 下载地址,不需要域名
 * @param params
 * @returns {Promise<unknown>}
 */
export const apiGetBlob = (url, params) => {
  return new Promise((resolve, reject) => {
    request({
      url: url,
      method: 'get',
      params: params,
      timeout: 30000,
      responseType: 'blob'
    }).then(res => {
      resolve(res)
    }).catch(res => {
      reject(res)
    })
  })
}
